<?php
if (!$_GET['id']) {
    echo "Bad Request";
} else {
    $id = $_GET['id'];
//including header.php 
    include '../../include/header.php';
//including page.php (It is a class which contain functions)
    include '../../libraries/page.php';
    $page = new page();
    session_start();
    $userid = $_SESSION['userid'];

//checking the session
    if (!$_SESSION['username']) {
        header("location:../user/login.php");
    }
    if ($_GET['q'] == 'logout') {
        $page->user_logout();
        header("location:../user/login.php");
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
// for $_POST['submit'].  Remember, field names are determined by array keys!
        $result = $page->mysql_update_array("products", $id, "productid", $_POST);
        if ($result) {
            header("location:products.php?status='1'");
        } else {
            $msg = $message[0];
        }
    }
    $products = 'active';
    include '../../include/navbar.php';
    ?>

<div class="bread_crumb" style="margin-left: 2%">
    &laquo;  <a href="products">Back</a>
  </div>

    <!----------------------------------Display the result message of Update--------------------->
    <div  align="center"><? echo $msg; ?></div>




    <!-----------------We are retrieving data from database table to display in the fields to update--------->
    <?
    //php code to get the data from products table 
    $products = mysql_query("select * from products where productid = $id");
    $products_data = mysql_fetch_array($products);

    $product_id = $products_data['productid'];
    $product_group_id = $products_data['groupid'];
    $name = ucwords(strtolower($products_data['name']));
    $code = $products_data['code'];
    $partno = $products_data['partno'];
    $oldpartno = $products_data['oldpartno'];
    $packing = $products_data['packing'];
    $price = $products_data['price'];
    $opqty = $products_data['opqty'];
    $active = $products_data['active'];
    //To get current date and time
    date_default_timezone_set('Asia/Calcutta');
    $date1 = date("Y-m-d H:i:s");
    ?>

    <!------------------------------form------------------------------------------------>
    <div class="maindiv">
        <form method="POST"  action="">
            <h3 style="color:#777;text-align: center"> Update Product</h3>
            <div class="fulldiv"><div class="group_select_mob">
                    <div class="btn-group"  style="float: right">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                            Action&nbsp;
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                            <!-- dropdown menu links -->
                            <li> <a href ="addcombination?id=<?= $product_id; ?>">Add combination</a></li>
                        </ul>
                    </div>

                </div></div>
            <div class="leftdiv">
                <label>Product name</label>
                <input class="input-xlarge" id="focusedInput" type="text" name="name" value="<? echo $name; ?>"  required>
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Group</label>
                <select name="groupid" style="width:285px"> 
                    <?
                    //php code to get the data from groups table 
                    $groups_actual = mysql_query("select * from groups where groupid = $product_group_id");
                    $groups_actual_data = mysql_fetch_array($groups_actual);
                    $groups_actual_id = $groups_actual_data['groupid'];
                    $groups_actual_name = ucwords(strtolower($groups_actual_data['name']));
                    ?>
                    <option  value="<? echo $groups_actual_id; ?>"><? echo $groups_actual_name; ?></option>

                    <?
                    //php code to get the data from groups table 
                    $groups = mysql_query("select * from groups");
                    while ($groups_data = mysql_fetch_array($groups)) {

                    $group_id = $groups_data['groupid'];
                    $name = ucwords(strtolower($groups_data['name']));
                    ?>
                    <option  value="<? echo $group_id; ?>"><? echo $name; ?></option>
                    <?
                    }
                    ?>
                </select>
            </div>
            <div class="leftdiv">
                <label>Code</label>
                <input class="input-xlarge"  id="focusedInput" name="code" type="text"  value="<? echo $code; ?>">
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Part No</label>
                <input class="input-xlarge"  id="focusedInput" name="partno" type="text"  value="<? echo $partno; ?>">
            </div>
            <div class="leftdiv">
                <label>Old Part No</label>
                <input class="input-xlarge"  id="focusedInput" name="oldpartno" type="text"  value="<? echo $oldpartno; ?>">
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Packing</label>
                <input class="input-xlarge" id="focusedInput" type="text" name="packing"  value="<? echo $packing; ?>">
            </div>
            <div class="leftdiv">
                <label>Price</label>
                <input class="input-xlarge" id="focusedInput" type="text" name="price"  value="<? echo $price; ?>">
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Opqty</label>
                <input class="input-xlarge" id="focusedInput" type="text" name="opqty"  value="<? echo $opqty; ?>">
                <input class="input-xlarge" id="focusedInput" type="hidden" name="m_user" value ="<? echo $userid; ?>"  >
                <input class="input-xlarge" id="focusedInput" type="hidden" name="m_date" value ="<? echo $date1; ?>"  >

            </div>
            <div class="fulldiv">
                <div class="btn1"><button class="btn btn-inverse"  type="submit" >Update Product</button></div>
            </div>
        </form>
        <span class="hide_button"><a href ="addcombination?id=<?= $product_id; ?>"><button style="height:30px;width:150px">Add combination</button></a></span>
    </div>

    <?php
//including footer.php
    include '../../include/footer.php';
    ?>
    <?php
}
?>
